package com.groupbuying.sysconfig.service.impl;

import com.groupbuying.sysconfig.constants.CommonConstants;
import com.groupbuying.sysconfig.constants.RedisCacheConstants;
import com.groupbuying.sysconfig.controller.CommonController;
import com.groupbuying.sysconfig.utils.R;
import com.groupbuying.sysconfig.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.groupbuying.sysconfig.dao.SysconfigOpexRuleDao;
import com.groupbuying.sysconfig.domain.SysconfigOpexRuleDO;
import com.groupbuying.sysconfig.service.SysconfigOpexRuleService;
import org.springframework.web.bind.annotation.RequestBody;


@Service
public class SysconfigOpexRuleServiceImpl implements SysconfigOpexRuleService {
    @Autowired
    private SysconfigOpexRuleDao sysconfigOpexRuleDao;

    @Autowired
    private CommonController commonController;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public SysconfigOpexRuleDO get(String ruleId) {
        return sysconfigOpexRuleDao.get(ruleId);
    }

    @Override
    public List<SysconfigOpexRuleDO> list(Map<String, Object> map) {
        return sysconfigOpexRuleDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return sysconfigOpexRuleDao.count(map);
    }

    @Override
    public int save(SysconfigOpexRuleDO sysconfigOpexRule) {
        return sysconfigOpexRuleDao.save(sysconfigOpexRule);
    }

    @Override
    public int update(SysconfigOpexRuleDO sysconfigOpexRule) {
        return sysconfigOpexRuleDao.update(sysconfigOpexRule);
    }

    @Override
    public int remove(String ruleId) {
        return sysconfigOpexRuleDao.remove(ruleId);
    }

    @Override
    public int batchRemove(String[] ruleIds) {
        return sysconfigOpexRuleDao.batchRemove(ruleIds);
    }

    @Override
    public R createOpexRule(@RequestBody Map<String, Object> params) {
        Object temp = params.get("storeSettlement");
        String storeSettlement = temp != null ? temp.toString() : null;
        temp = params.get("onlineExtracting");
        String onlineExtracting = temp != null ? temp.toString() : null;
        temp = params.get("backCurrencyRatio");
        String backCurrencyRatio = temp != null ? temp.toString() : null;
        temp = params.get("currencyExtracting");
        String currencyExtracting = temp != null ? temp.toString() : null;
        temp = params.get("currencyPayDiscount");
        String currencyPayDiscount = temp != null ? temp.toString() : null;

        if (StringUtils.isBlank(storeSettlement) || StringUtils.isBlank(onlineExtracting) || StringUtils.isBlank(backCurrencyRatio) || StringUtils.isBlank(currencyExtracting) || StringUtils.isBlank(currencyPayDiscount)) {
            return R.paramIsNull();
        }

        //校验比例
        //1.校验线上支付
        if (100 != Integer.parseInt(storeSettlement) + Integer.parseInt(onlineExtracting) + Integer.parseInt(backCurrencyRatio)) {
            return R.error("线上支付规则设置错误");
        }

        //2.校验积分支付
        if(Integer.parseInt(currencyPayDiscount) != Integer.parseInt(currencyExtracting) + Integer.parseInt(storeSettlement)){
            return R.error("积分支付规则设置错误");
        }

        //都通过
        SysconfigOpexRuleDO sysconfigOpexRuleDO = new SysconfigOpexRuleDO();
        sysconfigOpexRuleDO.setRuleId("ORNO" + commonController.createActivityId("",""));
        sysconfigOpexRuleDO.setDelFlag(CommonConstants.COMMON_FLAG_UNDEL);
        sysconfigOpexRuleDO.setGmtCreate(new Date());
        sysconfigOpexRuleDO.setGmtModified(new Date());
        sysconfigOpexRuleDO.setStoreSettlement(new BigDecimal(storeSettlement));
        sysconfigOpexRuleDO.setOnlineExtracting(new BigDecimal(onlineExtracting));
        sysconfigOpexRuleDO.setBackCurrencyRatio(new BigDecimal(backCurrencyRatio));
        sysconfigOpexRuleDO.setCurrencyExtracting(new BigDecimal(currencyExtracting));
        sysconfigOpexRuleDO.setCurrencyPayDiscount(new BigDecimal(currencyPayDiscount));

        if(this.save(sysconfigOpexRuleDO)>0){
            //清除缓存
            redisUtils.delKeyCache(RedisCacheConstants.CACHE_KEY_RULE_CONTENT);
            redisUtils.delKeyCache(RedisCacheConstants.CACHE_KEY_RULE_COUNT);
            return R.ok();
        }
        return R.error();
    }


}
